<template>
  <el-dialog
    :title="`${form.id ? '修改' : '增加'}角色信息`"
    :visible.sync="visible"
    :close-on-click-modal="false"
    width="500px"
  >
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item
        prop="roleName"
        label="角色名称"
        :rules="[{required: true, message: ' ', trigger: 'submit'}]">
        <el-input v-model="form.roleName" style="width:360px"></el-input>
      </el-form-item>
      <el-form-item
        prop="remark"
        label="角色备注"
      >
        <el-input
          v-model="form.remark"
          type="textarea"
          :rows="3"
          style="width: 360px"
        />
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary" @click="onSubmit">确认</el-button>
      <el-button type="default" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  data() {
    return {
      visible: false,
      form: {}
    };
  },
  methods: {
    open(role) {
      this.visible = true;
      this.role = role;
      if (this.$refs.form)
        this.$refs.form.clearValidate();
      this.form = {...role};
    },
    onSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;
        const ret = await this.$http.post('/api/role', this.form);
        if (ret.success) {
          this.visible = false;
          if (this.form.id) {
            Object.assign(this.role, this.form);
          } else {
            this.$vnode.context.$refs.table.reloadCurrentPage();
          }
          this.$message.success(`${this.form.id ? '修改' : '增加'}角色信息成功`);
        }
      });

    }
  }
}
</script>
